#include "ContraStateInWater.h"




ContraStateInWater::ContraStateInWater(ContraData* data)
{
	_data = data;

	_delaySpriteShooting = 40;
	_counterSpriteShooting = 41;
	isUp = false;
	isMoving = false;
}

bool ContraStateInWater::checkIsShooting()
{
	bool res = _counterSpriteShooting < _delaySpriteShooting;
	if (res == true) {
		_counterSpriteShooting++;
	} 
	return res;
}

void ContraStateInWater::stand()
{
	isMoving = false;
	_data->notMove = false;
	if (checkIsShooting()) return;
	// check current state to set stand left or right
	if (_data->getBodySprites()->state % 2 == 0) {
		_data->bodySprites->setState(_STATE_CONTRA_SWIM_LEFT);
	}
	else {
		_data->bodySprites->setState(_STATE_CONTRA_SWIM_RIGHT);
	}
}

void ContraStateInWater::standUp()
{
	_data->notMove = false;
	delayForUp = 10;
	isUp = true;
	if (checkIsShooting()) return;
	// check current state to set stand left or right
	if (_data->getBodySprites()->state % 2 == 0) {
		_data->bodySprites->setState(_STATE_CONTRA_SWIM_LEFT);
	}
	else {
		_data->bodySprites->setState(_STATE_CONTRA_SWIM_RIGHT);
	}
}

void ContraStateInWater::moveRight()
{
	isMoving = true;
	if (checkIsShooting()) {
		//_data->bodySprites->setState(_STATE_CONTRA_SWIM_RIGHT_FIRE);
		return;
	}
	_data->bodySprites->setState(_STATE_CONTRA_SWIM_RIGHT);
}

void ContraStateInWater::moveLeft()
{
	isMoving = true;
	if (checkIsShooting()) {
		//_data->bodySprites->setState(_STATE_CONTRA_SWIM_LEFT_FIRE);
		return;
	}
	_data->bodySprites->setState(_STATE_CONTRA_SWIM_LEFT);
}

void ContraStateInWater::fire()
{
	if (!_data->checkDelayShooting()) {
		// check contra are standing
		// reset timeDelayShooting
		_data->resetTimeDelayShooting();
		_counterSpriteShooting = 0;
		int bodyState = _data->bodySprites->state;
		if (!isUp && delayForUp <= 0) {
			if (_data->getBodySprites()->state % 2 == 0) {
				_data->bodySprites->setState(_STATE_CONTRA_SWIM_LEFT_FIRE);
				_data->bulletContainer->fire(_data->rect->getLeft() + 5, _data->rect->Y - 5, _BULLET_DIRECTION_LEFT, NULL);
			}
			else {
				_data->bodySprites->setState(_STATE_CONTRA_SWIM_RIGHT_FIRE);
				_data->bulletContainer->fire(_data->rect->getRight() - 5, _data->rect->Y - 5, _BULLET_DIRECTION_RIGHT, NULL);
			}	
		} else if (isUp) {
			if (isMoving) {
				fireCrossUp();
			} else  {
				fireUp();
			}
		}
	}
}

void ContraStateInWater::fireUp()
{
	if (_data->getBodySprites()->state % 2 == 0) {
		_data->bodySprites->setState(_STATE_CONTRA_SWIM_FIRE_UP_LEFT);
		_data->bulletContainer->fire(_data->rect->X - 5, _data->rect->getTop() + 20, _BULLET_DIRECTION_UP, NULL);
	}
	else {
		_data->bodySprites->setState(_STATE_CONTRA_SWIM_FIRE_UP_RIGHT);
		_data->bulletContainer->fire(_data->rect->X + 5, _data->rect->getTop() + 20, _BULLET_DIRECTION_UP, NULL);
	}
}

void ContraStateInWater::fireCrossUp()
{
	if (_data->getBodySprites()->state % 2 == 0) {
		_data->bodySprites->setState(_STATE_CONTRA_SWIM_FIRE_CROSS_LEFT);
		_data->bulletContainer->fire(_data-> rect->X - 20, _data->rect->getTop() - 20, _BULLET_DIRECTION_UP_LEFT, NULL);
	}
	else {
		_data->bodySprites->setState(_STATE_CONTRA_SWIM_FIRE_CROSS_RIGHT);
		_data->bulletContainer->fire(_data->rect->X + 20, _data->rect->getTop() - 20, _BULLET_DIRECTION_UP_RIGHT, NULL);
	}
}

void ContraStateInWater::sit()
{
	_data->bodySprites->setState(_STATE_CONTRA_SWIM_DOWN);
	_data->notMove = true;
}

void ContraStateInWater::update()
{
	//if (_data->jumpingTimeCounter > 0) fly();
	_data->bodySprites->updateCurrent();
	if (delayForUp > 0){
		delayForUp--; 
	} else {
		isUp = false;
	}
	_data->bulletContainer->update(0);

	if (_data->timeDelayShooting > 0) _data->timeDelayShooting--;
}

void ContraStateInWater::draw(long time)
{
	_data->bodySprites->draw();

	_data->bulletContainer->draw(0);
}
